package com.itheima.admin.listener;

import com.itheima.admin.service.WemediaNewsAutoScanService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.admin.listener
 */
@Component
@Slf4j // 后台自动运行
public class WemediaNewsAutoScanListener {

    @Autowired
    private WemediaNewsAutoScanService wemediaNewsAutoScanService;

    /**
     * 文章自动审核
     * @param msg  文章的id
     */
    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_AUTO_SCAN_TOPIC)
    public void wemediaNewsAutoAudit(ConsumerRecord<String,String> msg){
        // 消息所在分区  0 1 2 3
        int p = msg.partition();
        // 消息的offset下标
        long o = msg.offset();
        // 标识同一次业务的日志
        String uuid = UUID.randomUUID().toString();
        String value = msg.value();
        long wmNewsId = Long.parseLong(value);
        log.info("文章自动审核开始：{},{},{},{}", p,o,wmNewsId,uuid);
        // todo自动审核业务
        try {
            wemediaNewsAutoScanService.autoScanWemediaNewsById(wmNewsId,uuid);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("文章自动审核失败：{}", e, uuid);
        }
        log.info("文章自动审核完成:{}",uuid);
    }
}
